package com.xuzimian.demo.java.proxy.dynamic.performance

import com.xuzimian.demo.java.proxy.ISubject
import java.util.function.Supplier

object DynamicProxyPerformanceDemo {
    const val CIRCLE = 10000000
    var begin = System.currentTimeMillis()
    @Throws(Exception::class)
    @JvmStatic
    fun main(args: Array<String>) {
        executeProxyForTestPerformance("Jdk", Supplier { ProxyFactory.createJdkProxy() })
        executeProxyForTestPerformance("Cglib", Supplier { ProxyFactory.createCglibProxy() })
        executeProxyForTestPerformance("Javassist Dynamic", Supplier { ProxyFactory.createJavassistDynProxy()!! })
        executeProxyForTestPerformance("Javassist Bytecode", Supplier { ProxyFactory.createJavassistBytecodeDynamicProxy()!! })
    }

    @Throws(Exception::class)
    private fun executeProxyForTestPerformance(tag: String, createProxy: Supplier<ISubject>) {
        val principal = createProxy.get()
        println("Create " + tag + " Proxy :" + principal.javaClass.getName(), begin)
        resetBegin()
        for (i in 0 until CIRCLE) {
            principal.processBusiness()
        }
        println(" Call " + tag + " Proxy : " + (System.currentTimeMillis() - begin)+ "ms")
        resetBegin()
        println()
    }

    private fun println(msg: String, begin: Long) {
        print(msg + " - " + (System.currentTimeMillis() - begin) + "ms")
    }

    private fun resetBegin() {
        begin = System.currentTimeMillis()
    }
}